%include "nasm_macros.inc"
%include "os.inc"
section .text
BITS 16
org 100h
%define SEG0 0h
%define SEG1 1000h
%define OFF 7c00h
go:
%ifdef DEBUG
cld
mov si,go2
mov cx,600
mov di,7c00h
xor ax,ax
mov es,ax
rep movsb
jmp word 0:7c00h
%endif
go2:
DEB equ $ - go
cli
cld
xor ax,ax
mov ss,ax
mov sp,OFF - 100h
mov ds,ax
mov es,ax
sti
push edx
;j1:
;xor ax,ax
;int 16h
;call dump_regs
;jmp j1
mov al,3
int 10h
;
mov bp,OFF
cmp word [bp + 1feh - DEB],MBR_MAGIC
jne near exit
mov cl,4
lea si,[bp+PRT_START - DEB]
lo1:
lodsd
test al,80h ; active part?
jne jok
add si,16-4
loop lo1
jmp exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;
jok:
push eax
mov bx,55aah
mov ah,41h
int 13h
pop eax
jc nolba
cmp bx,MBR_MAGIC
jne nolba
test cl,1
jz nolba
lodsd 
call nx0
lba_packet:
    db 0x10 ;18h size of packet
    db 0    ;rsrv
    dw 0    ;nsec            
    dw 0,SEG1 ;off:seg of buffer
	dd 0 ;lba offset of start sector
    dd 0 ;not used
nx0:
pop di
push di
add di,8
movsd
lodsd ;nsec
sub di,4+4+2 ;nsec
stosw
mov ax,4200h
pop si
int 13h
jc exit
os:
pop edx ;restore cpu info
jmp word SEG1:0
;
nolba:
push SEG1
pop es
mov al,0
rol eax,16
mov edi,eax
lodsd
lodsd
lodsd ;nsec
mov bp,ax
xor bx,bx
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;es:bx=buf,edi=1st chs,bp=nsec,dx=disk ;No more then 1 cyl
wdlr:
mov cx,di
rol edi,16
mov ax,di
mov dh,al
rol edi,16
;
mov ah,02h
mov al,1
push es
int 13h
pop es
jnc jnc0
call reset
jmp wdlr
jnc0:
inc di
add bx,SECT_SZ
dec bp
jnz wdlr
jmp os

exit:
cli
hlt
jmp $
;;;;;;;;;;
;ds:ax=msg; -->ax=off of end
print:
push si
mov si,ax
mov ah,0eh
prbk:
lodsb
or al,al
je prex
int 10h
jmp prbk
prex:
mov ax,si
pop si
ret
;
dbg:
push bx
call nxd
db '0',13,10,0
nxd:
pop bx
mov ax,bx
call print
inc byte [bx]
pop bx
ret


reset:
pusha
xor dx,dx
xor ax,ax
int 13h
popa
ret

;
dump_regs:
;8*4=32; +2
push ebp ;+4
mov ebp,esp ;esp real=ebp +6; ebp real=[ebp]
push eax
push ebx
push ecx
push edx
push esi
push edi
lea eax,[ebp + 6]
push eax ;esp
push dword [ebp] ;ebp
movzx eax,word [ebp+4]
push eax ;ret
;
push ds
push cs
pop ds
mov bx,9
mov si,bp
dec si; hi byte of eax
call nx1
msg_regs db 'EAX=',0,'EBX=',0,'ECX=',0,'EDX=',0,13,10,'ESI=',0,'EDI=',0,'ESP=',0,'EBP=',0,13,10,'RET=',13,10,0
nx1:
pop di
drl:
mov ax,di
call print
mov di,ax
mov cx,4
drl2:
std
xor eax,eax
lodsb
cld
call h2a
push eax
mov ax,sp
call print
pop eax
loop drl2
call nx2
db ' ',0
nx2:
pop ax
call print
dec bx
jnz drl
pop ds
add sp,3*4
;xor ax,ax
;int 16h
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax ;spoiled
pop ebp 
ret

;al-->ax=ASCII
h2a:
rol eax,16
xor ax,ax
rol eax,16
shl ax,4
shr al,4
cmp al,9
jbe hs1
add al,7
hs1:
add al,30h
;
cmp ah,9
jbe hs2
add ah,7
hs2:
add ah,30h
;
xchg al,ah
ret

;****f***DATA


SZ1 equ $ - go + 2 + 4*4*4
%if SZ1 < SECT_SZ
	times  (SECT_SZ - SZ1) db 0
%endif
	;partition: 
part_table:
;db 80h,0,2,0,70,0,3,0
;db 01,0,0,0,01,0,0,0

dd 0,0,0,0
dd 0,0,0,0
dd 0,0,0,0
dd 0,0,0,0
magic:
dw 0aa55h
